package ms.unicorn.diagram.figures;

import ms.unicorn.dsl.IEditPartAware;
import ms.unicorn.dsl.IFigureEx;
import ms.unicorn.dsl.ITheme;
import ms.unicorn.util.FigureUtil;

import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.draw2d.ui.figures.IAnchorableFigure;
import org.eclipse.gmf.runtime.gef.ui.figures.DefaultSizeNodeFigure;

public class EntityPlateFigure extends DefaultSizeNodeFigure implements IFigureEx, IEditPartAware {

	private EditPart editpart;
	private IAnchorableFigure fig;
	private String name;
	private int flag;
	private ITheme theme;
	
	
	
	public EntityPlateFigure(String name, Dimension defSize) {
		super(defSize);
		this.name = name;
	}
	
	public EditPart getEditPart() {
		return editpart;
	}
	
	public void setEditPart(EditPart editpart) {
		this.editpart = editpart;
	}
	
	@Override
	public ConnectionAnchor getConnectionAnchor(String terminal) {
		if (getAnchorFigure() != null)
			return getAnchorFigure().getConnectionAnchor(terminal);
		return super.getConnectionAnchor(terminal);
	}
	
	@Override
	public String getConnectionAnchorTerminal(ConnectionAnchor c) {
		if (getAnchorFigure() != null)
			return getAnchorFigure().getConnectionAnchorTerminal(c);
		return super.getConnectionAnchorTerminal(c);
	}
	
	@Override
	public ConnectionAnchor getSourceConnectionAnchorAt(Point p) {
		if (getAnchorFigure() != null)
			return getAnchorFigure().getSourceConnectionAnchorAt(p);
		return super.getSourceConnectionAnchorAt(p);
	}
	
	@Override
	public ConnectionAnchor getTargetConnectionAnchorAt(Point p) {
		if (getAnchorFigure() != null)
			return getAnchorFigure().getTargetConnectionAnchorAt(p);
		return super.getTargetConnectionAnchorAt(p);
	}
	
	protected IAnchorableFigure getAnchorFigure() {
		if (fig == null) {
			 IFigure temp = FigureUtil.getChildFigure(this, IFigureEx.FLAG_CONNECTOR_ANCHOR);
			 fig = temp instanceof IAnchorableFigure ? (IAnchorableFigure)temp : this;
		}
		return fig != this ? fig : null;
	}

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public int getFlag() {
		return this.flag;
	}

	@Override
	public void setFlag(int flag) {
		this.flag = flag;
	}

	@Override
	public ITheme getTheme() {
		return this.theme;
	}

	@Override
	public void setTheme(ITheme theme) {
		this.theme = theme;
	}

}
